package edu.pku.sei.mte.ui;

import java.io.OutputStream;
import java.io.PrintStream;

import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;

public class GUIPrintStream extends PrintStream {

	private StyledText text;
	private Color color;

	public GUIPrintStream(OutputStream out, StyledText t, Color c) {
		super(out);
		text = t;
		color = c;
	}

	@Override
	public void write(byte[] buf, int off, int len) {
		final String message = new String(buf, off, len);
		int start = text.getCharCount();
		text.append(message);
		text.setStyleRange(new StyleRange(start, message.length(), color, null));
		text.setTopIndex(text.getVerticalBar().getMaximum());
//		SwingUtilities.invokeLater(new Runnable() {
//			public void run() {
//				sb.append(message);
////				if(sb.length() > text.getTextLimit())
////					sb.delete(0, sb.length()-text.getTextLimit());
//				text.setText(sb.toString());
//			}
//		});
	}
}
